// 获取文件url，require不能用
export const getUrl = (url: any) => new URL(url, import.meta.url).href
// 范围随机数
export const rangeRandom = (n: number, m: number): number => {
  return Math.random() * (m - n) + n
}
// 延迟
// export const delay = (s: number) => new Promise((resolve) => setTimeout(resolve, s))
let timeoutInterval: any = null
export const delay = (s: number) => new Promise((resolve) => {
  if (timeoutInterval) clearTimeout(timeoutInterval)
  timeoutInterval = setTimeout(resolve, s)
  return timeoutInterval
})
// 默认时间
export const defaultTime = [new Date(0, 0, 0, 0, 0, 0), new Date(0, 0, 0, 23, 59, 59)]

/**
 * 判断用户是否长时间无操作，比如30s
 * @param {() => void} callback
 * @param {boolean} immediate false，是否立即开始
 * @param {number} timeout 30，单位s
 */
export const pageDeactivated = async (callback: () => void, immediate = false, timeout = 30) => {
  let pageTimer: any
  let beginTime = 0
  // 清除定时器
  const onClearTimer = () => {
    pageTimer && clearTimeout(pageTimer)
    pageTimer = undefined
  }
  // 开始定时器
  const onStartTimer = () => {
    const currentTime = +new Date()
    if (pageTimer && currentTime - beginTime < 100) {
      return
    }
    onClearTimer()
    beginTime = currentTime
    pageTimer = setTimeout(() => {
      callback && callback()
    }, timeout * 1000)
  }

  const onPageVisibility = () => {
    onClearTimer()
    if (document.visibilityState === 'visible') {
      const currentTime = +new Date()
      // 页面显示时，计算时间，超出则直接执行callback
      if (currentTime - beginTime >= timeout * 1000) {
        callback && callback()
        return
      }
      // 继续计时
      pageTimer = setTimeout(() => {
        callback && callback()
      }, timeout * 1000 - (currentTime - beginTime))
    }
  }

  const onStart = () => {
    onStartTimer()
    document.addEventListener('mousedown', onStartTimer)
    document.addEventListener('mousemove', onStartTimer)
    document.addEventListener('visibilitychange', onPageVisibility)
  }

  const onStop = () => {
    onClearTimer()
    document.removeEventListener('mousedown', onStartTimer)
    document.removeEventListener('mousemove', onStartTimer)
    document.removeEventListener('visibilitychange', onPageVisibility)
  }

  if (immediate) {
    onStart()
  }
  return {
    onStart,
    onStop,
    onClearTimer
  }
}

/**
 * map模式 ，数组转tree
 * @param array 目标数组
 * [
    { id: "01", name: "张大大", pid: "", job: "项目经理" },
    { id: "02", name: "小亮", pid: "01", job: "产品leader" },
    { id: "03", name: "小丽", pid: "02", job: "产品经理" },
    { id: "04", name: "大光", pid: "02", job: "产品经理" },
  ]
 * @param pid 第一级 目标id
 * @returns {*[]}
 * '[{"id":"01","name":"张大大","pid":"","job":"项目经理","children":[{"id":"02","name":"小亮","pid":"01","job":"产品leader","children":[{"id":"03","name":"小丽","pid":"02","job":"产品经理","children":[]},{"id":"04","name":"大光","pid":"02","job":"产品经理","children":[]}]}]}]'
 * @constructor
 */
export const arrayToTree = (array: any[], pid: string = 'pid') => {
  if (!Array.isArray(array) || !array.length) return []
  const treeMap: any = {}
  const tree: any[] = []

  array.forEach(item => {
    treeMap[item.id] = { ...item, children: [] }
    if (!treeMap[item[pid]]) {
      treeMap[item[pid]] = { children: [] }
    }
    if (!item[pid]) {
      tree.push(treeMap[item.id])
    } else {
      treeMap[item[pid]].children.push(treeMap[item.id])
    }
  })

  return tree
}